package com.bood.shimmer.config;

import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * MybatisPlus 配置
 * </p>
 *
 * @author：bood
 * @date：2020/9/25
 */
@Configuration
@MapperScan(basePackages = {"com.bood.shimmer.modules.*.mapper"})
public class MybatisPlusConfig {

    /**
     * <p>
     * 分页插件
     * </p>
     *
     * @return：com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor
     * @author：bood
     * @date：2020/9/25
     */
    @Bean
    @ConditionalOnMissingBean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作， true 调回到首页，false 继续请求  默认 false
        paginationInterceptor.setOverflow(true);

        List<ISqlParser> sqlParserList = new ArrayList<>();
        // 攻击 SQL 阻断解析器、加入解析链 作用！阻止恶意的全表更新删除
        sqlParserList.add(new BlockAttackSqlParser());
        paginationInterceptor.setSqlParserList(sqlParserList);

        return paginationInterceptor;
    }

    /**
     * <p>
     * 乐观锁插件
     * </p>
     *
     * @return：com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor
     * @author：bood
     * @date：2020/9/25
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

}